//
//  ZYCSelectFutureDateView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/27.
//

#import "ZYCSelectFutureDateView.h"
#define kDateViewH 258
#define kPickViewH 218
@interface ZYCSelectFutureDateView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIView *dimView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIPickerView *pickView;

@property (nonatomic, strong) NSArray *yearArr;
@property (nonatomic, strong) NSArray *monthArr;
@property (nonatomic, strong) NSArray *dayArr;

@property (nonatomic, assign) NSInteger currentYear;
@property (nonatomic, assign) NSInteger currentMonth;
@property (nonatomic, assign) NSInteger currentDay;
@property (nonatomic, assign) NSInteger totalDay;

@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, assign) NSInteger day;



@end
@implementation ZYCSelectFutureDateView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.00f];
        [self setupUI];
        [self setupConstraint];
        [self loadDate];
    }
    return self;
}

#pragma mark - UI及约束
- (void)setupUI{
    [self addSubview:self.dimView];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.confirmBtn];
    [self.baseView addSubview:self.pickView];
    [self.baseView addSubview:self.lineView];
    [self.baseView addSubview:self.titleLabel];
}

- (void)setupConstraint{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.baseView.mas_top).offset(20);
        make.centerX.equalTo(self);
    }];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.baseView.mas_top);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);
        make.top.equalTo(self.baseView.mas_top);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
   
}
- (void)loadDate{
    NSDate* now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour;
    comps = [calendar components:unitFlags fromDate:now];
    
    _currentYear= [comps year];
    _currentMonth = [comps  month];
    _currentDay = [comps day];
    
    //当月总天数
    _totalDay = [self calculationThisMonthDays:now];
    
    _yearArr = [self getYearWithYear:_currentYear];
    _year = [[_yearArr firstObject] intValue];
    [self.pickView reloadComponent:0];
    [self.pickView selectRow:0 inComponent:0 animated:YES];
    //_yearArr.count-1
    _monthArr = [self getMonthWithYear:_currentYear];
    _month = [[_monthArr firstObject] intValue];
    [self.pickView reloadComponent:1];
    [self.pickView selectRow:0 inComponent:1 animated:YES];
    //_monthArr.count-1
    _dayArr = [self getDayWithYear:_currentYear andMonth:_currentMonth];
    _day = [[_dayArr firstObject] intValue];
    [self.pickView reloadComponent:2];
    [self.pickView selectRow:0 inComponent:2 animated:YES];
    //_dayArr.count-1
}

- (void)clickCancelAction:(UIButton *)sender{
    [self dismiss];
}
- (void)clickComfirmAction:(UIButton *)sender{
    [self dismiss];
    NSString *dateString = [NSString stringWithFormat:@"%d-%.2d-%.2d",_year,_month,_day];
    if ([self.delegate respondsToSelector:@selector(didSelectedDateString:)]) {
        [self.delegate didSelectedDateString:dateString];
    }
}
#pragma mark - getters

- (UIPickerView *)pickView {
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kPickViewH)];
        _pickView.backgroundColor = HEX_RGB(@"FFFFFF");
        _pickView.delegate = self;
        _pickView.dataSource = self;
    }
    return _pickView;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 1)];
        _lineView.backgroundColor = HEX_RGB(@"EEEEEE");;
    }
    return _lineView;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:HEX_RGB(@"#A3A3A3") forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kCustomFont(@"PingFangSC-Medium", 15.0f);
        [_cancelBtn addTarget:self action:@selector(clickCancelAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [[UIButton alloc] init];
        _confirmBtn.titleLabel.font = kCustomFont(@"PingFangSC-Medium", 15.0f);
        [_confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:HEX_RGB(@"#2384D8") forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(clickComfirmAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}


- (UIView *)dimView{
    if (!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.3;
        _dimView.backgroundColor = [UIColor blackColor];
    }
    return _dimView;
}

- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kDateViewH)];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    switch (component) {
        case 0:
            return _yearArr.count;
            break;
        case 1:
            return _monthArr.count;
            break;
        case 2:
            return _dayArr.count;
            break;
        default:
            break;
    }
    return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    switch (component) {
        case 0:
            {
                return [NSString stringWithFormat:@"%@年",_yearArr[row]];
            }
            break;
        case 1:
        {
             return [NSString stringWithFormat:@"%@月",_monthArr[row]];
        }
            break;
        case 2:
        {
           return [NSString stringWithFormat:@"%@日",_dayArr[row]];
        }
            break;
        default:
            return @"";
            break;
    }
}

// 设置row字体，颜色
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel* pickerLabel            = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel                 = [[UILabel alloc] init];
        pickerLabel.textAlignment   = NSTextAlignmentCenter;
        pickerLabel.backgroundColor = [UIColor clearColor];
        pickerLabel.font            = kSystemFont(16);
    }
    
    pickerLabel.text                = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:
        {
            _year = [_yearArr[row] intValue];
            _monthArr = [self getMonthWithYear:_year];
            _month = [[_monthArr firstObject] intValue];
            [self.pickView reloadComponent:1];
            //_monthArr.count-1
            [self.pickView selectRow:0 inComponent:1 animated:YES];
            _dayArr = [self getDayWithYear:_year andMonth:_month];
            _day = [[_dayArr firstObject] intValue];
            [self.pickView reloadComponent:2];
            [self.pickView selectRow:0 inComponent:2 animated:YES];
            // _dayArr.count-1
        }
            break;
        case 1:
        {
            _month = [_monthArr[row] intValue];
            _dayArr = [self getDayWithYear:_year andMonth:_month];
            _day = [[_dayArr firstObject] intValue];
            [self.pickView reloadComponent:2];
            [self.pickView selectRow:0 inComponent:2 animated:YES];
            //dayArr.count-1
        }
            break;
        case 2:
        {
            _day = [_dayArr[row] intValue];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark - 显示与隐藏
- (void)show{
    self.dimView.hidden = NO;
    self.hidden = NO;
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight-kDateViewH, kScreenWidth, kScreenHeight-kDateViewH);
    }];
}
- (void)dismiss{
    
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight-kDateViewH);
    }completion:^(BOOL finished) {
        self.dimView.hidden = YES;
        self.hidden = YES;
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


#pragma mark - private method
- (NSArray *)getYearWithYear:(NSInteger )year{
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSInteger i=0; i<2; i++) {
        [tempArr addObject:@(year+i)];
    }
//    NSArray *arr = [[tempArr.copy reverseObjectEnumerator] allObjects];
    return tempArr.copy;
}

- (NSArray *)getMonthWithYear:(NSInteger )year
{
    NSMutableArray *tempArr = [NSMutableArray array];
    if (_currentYear == year) {
        for (NSInteger i=_currentMonth; i<=12; i++) {
            [tempArr addObject:[NSString stringWithFormat:@"%.2ld",(long)i]];
        }
    }else{
        for (NSInteger i=1; i<=12; i++) {
            [tempArr addObject:[NSString stringWithFormat:@"%.2ld",(long)i]];
        }
    }

    return tempArr.copy;
}

- (NSArray *)getDayWithYear:(NSInteger)year andMonth:(NSInteger)month{
    
    NSMutableArray *temp1 = [NSMutableArray array];
    for (NSInteger i=1; i<=28; i++) {
        [temp1 addObject:[NSString stringWithFormat:@"%.2ld",(long)i]];
    }
    
    NSMutableArray *temp2 = [NSMutableArray array];
    for (NSInteger i=1; i<=29; i++) {
        [temp2 addObject:[NSString stringWithFormat:@"%.2ld",i]];
    }
    
    NSMutableArray *temp3 = [NSMutableArray array];
    for (NSInteger i=1; i<=30; i++) {
        [temp3 addObject:[NSString stringWithFormat:@"%.2ld",i]];
    }
    
    NSMutableArray *temp4 = [NSMutableArray array];
    for (NSInteger i=1; i<=31; i++) {
        [temp4 addObject:[NSString stringWithFormat:@"%.2ld",i]];
    }
    

    if (year == _currentYear&&month==_currentMonth) {
        NSMutableArray *temp5 = [NSMutableArray array];
//        for (int i=1; i<=_currentDay; i++) {
//            [temp5 addObject:[NSString stringWithFormat:@"%.2d",i]];
//        }
        for (NSInteger i= _currentDay; i<=_totalDay; i++) {
            [temp5 addObject:[NSString stringWithFormat:@"%.2d",i]];
        }
        return temp5.copy;
        
    }else{
        switch (month) {
            case 1:
                return temp4.copy;
                break;
            case 2:
                if ((year%4==0 && year %100 !=0) || year%400==0) {
                    return temp2.copy;
                }else{
                    return temp1.copy;
                }
                break;
            case 3:
                return temp4.copy;
                break;
            case 4:
                return temp3.copy;
                break;
            case 5:
                return temp4.copy;
                break;
            case 6:
                return temp3.copy;
                break;
            case 7:
                return temp4.copy;
                break;
            case 8:
                return temp4.copy;
                break;
            case 9:
                return temp3.copy;
                break;
            case 10:
                return temp4.copy;
                break;
            case 11:
                return temp3.copy;
                break;
            case 12:
                return temp4.copy;
                break;
            default:
                return temp4.copy;
                break;
        }
    }

}

- (NSInteger)calculationThisMonthDays:(NSDate *)days{
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:days];
    return range.length;
}
@end
